﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Net;
using System.Net.Mail;
using System.IO;


/// <summary>
/// Summary description for Helper
/// </summary>
public class Helper
{
	public Helper()
	{
		
	}

    public static string GetConnectionString()
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultDatabase"].ConnectionString;
        if (connectionString == null || connectionString.Length == 0)
            throw new ArgumentNullException("connectionString");
        return connectionString;
    }

    /// <summary>
    /// connect to defaul database.
    /// It throw a exception if a error appeared.
    /// </summary>
    /// <returns>a opened SqlConnection</returns>
    public static SqlConnection ConnectToDataBase()
    {
        return ConnectToDataBase("DefaultDatabase");
    }

    /// <summary>
    /// opened a SqlConnection to the database with the "name".
    /// It throw a exception if a error appeared.
    /// </summary>
    /// <param name="name"> name of database, that you want to connect.</param>
    /// <returns>a opened SqlConnection</returns>
    public static SqlConnection ConnectToDataBase(string name)
    {
        SqlConnection sqlConn = new SqlConnection(GetConnectionString());
        if (sqlConn != null)
        {
            sqlConn.Open();
            return sqlConn;
        }
        
        throw new ArgumentNullException("Connect to Database make an exception.");
    }

    public static void CloseSqlConnection(SqlConnection conn)
    {
        conn.Close();
        conn.Dispose();
    }

    public static void DisposeCommand(ref SqlCommand cmd)
    {
        try
        {
            cmd.Connection.Close();
            cmd.Connection.Dispose();
            cmd.Dispose();
        }
        catch
        {
        }
    }

    public static bool SendGMail(string to, string subject, string message)
    {
        try
        {
            return SendGMail("daugia.info@gmail.com", "vonguyenhiep", to, subject, message);
        }
        catch (Exception)
        {
            return false;
        }
        return false;
    }

    public static bool SendGMail(string gMailAccount, string password, string to, string subject, string message)
    {
        try
        {
            NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(gMailAccount);
            msg.To.Add(new MailAddress(to));
            msg.Subject = subject;
            msg.Body = message;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;
            client.Send(msg);

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    /// <summary>
    ///  get a random string, defaul 12 lengh.
    /// </summary>
    /// <returns>string</returns>
    public static string GetRandomString()
    {
        string path = Path.GetRandomFileName();
        path = path.Replace(".", ""); // Remove period.
        return path;
    }

}

